﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OurFAS.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Configuration;


namespace OurFAS.MySystem
{
    public partial class FrmLogin : Form
    {
        #region 构造函数
        public FrmLogin()
        {
            InitializeComponent();

            MyCommon.CommonHelper.ServerAddress = ConfigurationSettings.AppSettings["ServerAddress"].ToString();
            tbServerAddress.Text = MyCommon.CommonHelper.ServerAddress;
            if (!ConnectTest())
            {
                MessageBox.Show("远程数据库无法连接！");

                label3.Visible = true;
                tbServerAddress.Visible = true;
                btnConnectTest.Visible = true;
                label4.Visible = true;
            }
        }
        #endregion

        #region 控件函数
        private void tbUserPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                Login(tbUserName.Text, tbUserPwd.Text);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            Login(tbUserName.Text, tbUserPwd.Text);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
        #endregion

        #region 私有函数
        //登录验证
        private void Login(string usercode, string userpwd)
        {
            this.Visible = false;
            if (string.IsNullOrWhiteSpace(usercode))
            {
                this.Visible = true;
                MessageBox.Show("用户名不能为空!");
                return;
            }

            if (!DataHelper.LoginVerify(usercode, userpwd))
            {
                this.Visible = true;
                MessageBox.Show("登录失败!");
                return;
            }
            //
            //
            {
                //保存设置
                XmlDocument doc = new XmlDocument();
                //获得配置文件的全路径
                string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "OurFAS.exe.config";
                doc.Load(strFileName);
                //找出名称为“add”的所有元素
                XmlNodeList nodes = doc.GetElementsByTagName("add");
                for (int i = 0; i < nodes.Count; i++)
                {
                    //获得将当前元素的key属性
                    XmlAttribute att = nodes[i].Attributes["key"];
                    //根据元素的第一个属性来判断当前的元素是不是目标元素
                    if (att != null && att.Value == "ServerAddress")
                    {
                        //对目标元素中的第二个属性赋值
                        att = nodes[i].Attributes["value"];
                        att.Value =MyCommon.CommonHelper.ServerAddress;
                        break;
                    }
                }
                //保存上面的修改
                doc.Save(strFileName);

            }
            Base_Account account = DataHelper.GetAccountByUsercode(usercode);
            if (account == null)
            {
                this.Visible = true;
                MessageBox.Show("登录失败!");
                return;
            }
            FrmMain.CurrentUser = account;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        #endregion

        private void btnConnectTest_Click(object sender, EventArgs e)
        {
            MyCommon.CommonHelper.ServerAddress = tbServerAddress.Text;
            if (!ConnectTest())
            {
                MessageBox.Show("无法连接！");
            }
            else
            {
                MessageBox.Show("连接成功！");
            }
        }

        private bool ConnectTest()
        {
            SqlConnection conn = new SqlConnection(string.Format("Data Source={0};database=OurFasDB;uid=dftech2012;pwd=barcodesupply", MyCommon.CommonHelper.ServerAddress));
            try
            {
                conn.Open();
                conn.Close();
                return true;
            }
            catch
            {
                return false;

            }
        }
    }
}
